<script setup>
import { ref } from 'vue'
import { loginApi } from '@/apis/admin-user'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
const router = useRouter() // 用于路由跳转

const userStore = useUserStore()
const formData = ref({
  username: 'admin',
  password: '123456'
})
const formRef = ref(null)
const formRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 5, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
  ]
}

const onLogin = async () => {
  if (!isAgree.value) {
    ElMessage({ message: '请先勾选同意协议', type: 'warning' })
    return
  }
  await formRef.value.validate()
  const res = await loginApi(formData.value)
  userStore.setToken(res.data.data)
  ElMessage({ message: '登录成功', type: 'success' })
  router.replace({
    path: '/'
  })
}

// 判断是否同意协议
const isAgree = ref(false)
const handlerAgree = (val) => {
  isAgree.value = val
}
</script>
<template>
  <div class="page">
    <el-row>
      <el-col :span="8"></el-col>
      <el-col :span="7" class="login-box">
        <div class="login-form">
          <div class="title">
            <img
              src="../../assets/image/logo.png"
              alt=""
              style="border-radius: 50%; width: 10rem"
            />
          </div>
          <el-form
            label-position="top"
            :model="formData"
            :rules="formRules"
            ref="formRef"
            label-width="auto"
          >
            <el-form-item label="账号" required prop="username">
              <el-input
                v-model="formData.username"
                autocomplete="off"
                style="height: 2.2rem"
              />
            </el-form-item>
            <el-form-item label="密码" required prop="password">
              <el-input
                style="height: 2.2rem"
                v-model="formData.password"
                type="password"
                autocomplete="off"
              />
            </el-form-item>
          </el-form>

          <el-checkbox @change="handlerAgree" v-model="isAgree">
            <p class="tongyi">登录表示您已同意 <span>《服务条款》</span></p>
          </el-checkbox>

          <el-button
            class="but"
            @click="onLogin"
            style="background-color: #5e9cdd; color: #fefefe"
            >立即登录</el-button
          >
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.page {
  background: url('../../assets/image/bgc-image.jpg') center/cover;
  .login-box {
    height: 100vh;

    display: flex;
    flex-direction: column;
    justify-content: center;

    .login-form {
      padding: 0 2.5rem;
      height: 24.25rem;
      box-shadow: 0 0.25rem 0.5rem rgba(0, 0, 0, 0.2);
      background-color: #fefefe;
      border-radius: 10px;
      .title {
        display: flex;
        align-items: center;
        justify-content: center;
        //color: #479EE3;
        img {
          width: 6.25rem;
          margin-right: 1.25rem;
        }
      }
    }

    .tongyi {
      font-size: 0.75rem;

      span {
        color: blue;
      }
    }

    .but {
      margin: 0.625rem 0;
      background-color: #52abad;
      width: 100%;
      height: 2.5rem;
    }
  }
}
</style>
